{% extends "../../_basic.twig" %}

{% block header %}
    <link rel="stylesheet" href="./index.css?v={{random()}}">
    <style>
        auc-list-menu {
            width: 230px;
        }
    </style>
{% endblock %}

{% block body %}
    <div>      
        <h3>默认</h3> 
        <auc-list-menu id="menu" class="open">
            <auc-item id="a">item 1</auc-item>
            <auc-item>
                item 2
                <auc-list-menu> 
                    <auc-item>item 2.1</auc-item>
                </auc-list-menu>      
            </auc-item>                        
            <auc-gutter></auc-gutter>
            <auc-item>item 3</auc-item>
        </auc-list-menu>              
    </div>
    <script>

function hover(el, onOver, onOut) {
  var x, y, pX, pY;
  var h = {},
    state = 0,
    timer = 0;

  var options = {
    sensitivity: 7,
    interval: 100,
    timeout: 0
  };

  function delay(el, e) {
    if (timer) timer = clearTimeout(timer);
    state = 0;
    return onOut.call(el, e);
  }

  function tracker(e) {
    x = e.clientX;
    y = e.clientY;
  }

  function compare(el, e) {
    if (timer) timer = clearTimeout(timer);
    if ((Math.abs(pX - x) + Math.abs(pY - y)) < options.sensitivity) {
      state = 1;
      return onOver.call(el, e);
    } else {
      pX = x;
      pY = y;
      timer = setTimeout(function() {
        compare(el, e);
      }, options.interval);
    }
  }

  // Public methods
  h.options = function(opt) {
    options = Object.assign({}, options, opt);
    return h;
  };

  function dispatchOver(e) {
    if (timer) timer = clearTimeout(timer);
    el.removeEventListener('mousemove', tracker, false);

    if (state !== 1) {
      pX = e.clientX;
      pY = e.clientY;

      el.addEventListener('mousemove', tracker, false);

      timer = setTimeout(function() {
        compare(el, e);
      }, options.interval);
    }

    return this;
  }

  function dispatchOut(e) {
    if (timer) timer = clearTimeout(timer);
    el.removeEventListener('mousemove', tracker, false);

    if (state === 1) {
      timer = setTimeout(function() {
        delay(el, e);
      }, options.timeout);
    }

    return this;
  }

  h.remove = function() {
    if (!el) return;
    el.removeEventListener('mouseover', dispatchOver, false);
    el.removeEventListener('mouseout', dispatchOut, false);
  };

  if (el) {
    el.addEventListener('mouseover', dispatchOver, false);
    el.addEventListener('mouseout', dispatchOut, false);
  }

  return h;
};            

            let currentShowSubMenu = null;
            
            function liveCheck(el, s, cb) {
                function done() {
                    el.removeEventListener("mouseout", self);
                    s.removeEventListener("mouseover", toTargetIn);
                    s.removeEventListener("mouseout", toTargetOf);                            
                    el.__lock = null;
                }                
                function toTargetIn(e) {
                     el.__totarget = true;
                }
                function toTargetOf(e) {
                    el.__totarget = false;                                          
                    cb(e, done);
                }
                if (!el.__lock) {
                    el.__totarget = false;
                    el.addEventListener("mouseout", function(e) {
                        var self = this;     
                        if (!el.__lock) {
                            setTimeout(function() {
                               if (!el.__totarget) {
                                    cb(e, done);
                               }                            
                            }, 170);                            
                            el.__lock = true;
                        }
                    });
                    s.addEventListener("mouseover", toTargetIn);
                    s.addEventListener("mouseout", toTargetOf);
                }
            }

            function onItem(e, menu, idnex) {
                // if ( document.querySelector("#menu").auiIsChildren(e.target)){
                    if (currentShowSubMenu) {                
                        currentShowSubMenu.auiRemoveOnceClass("open")
                        currentShowSubMenu = null;
                        // console.log("remove");
                    }

                    if (!menu.___statslock) {
                        menu.___statslock = true;
                        function check(e) {
                            return function() {
                                if (menu.___statslock) {
                                    var s = menu.auiFindSelector("> auc-list-menu");
                                    if (s) {
                                        currentShowSubMenu = s;
                                        if (!s.classList.contains("open")) {
                                            s.auiAddOnceClass("open")
                                            liveCheck(menu, s, function(mouseoutevent, done) {
                                                if (s) {                                
                                                    s.auiRemoveOnceClass("open");
                                                    done();
                                                } else {
                                                    done();
                                                }
                                            });
                                        }
                                    } 
                                }
                            }
                        }
                        let n = check(e);
                        setTimeout(function() {
                            n();
                        }, 30);
                    }
               // }
            }
            

            function createSubmenuDetect(el, selector) {
                var menus = [];
                var ret = {};
                ret.remove = function(cb) {
                    menus.forEach(function(menu) {
                        menu.$hovercontrol.remove();
                        menu.$hovercontrol = null;
                    });
                    if (cb) {
                        cb();
                    }
                }
                ret.reset = function() {
                    ret.remove(function() {
                        ret.init();
                    });
                }
                ret.init = function() {
                    menus = Array.prototype.slice.call(el.auiFindSelectorAll(selector));
                    menus.forEach(function(menu, index) {
                        menu.$hovercontrol = hover(menu, function(e) {
                            onItem(e, menu, index);
                        }, function(e) {
                            menu.___statslock = false;
                        });
                    });
                }
                return ret;
            }

         var menu = document.querySelector("#menu");
            menu.$control = createSubmenuDetect(menu, "> auc-item");

            {# hover(document.body, function() {
                menu.$control.init();
            }, function() {
                menu.$control.remove();
            }); #}

    </script>
{% endblock %}
